<template>
  <div class="container">
    <el-card
      v-loading="loading"
      element-loading-text="拼命加载中....."
      element-loading-background="rgba(0,0,0,0.5)"
      >
      <!-- 搜索 -->
      <div class="serveInput">
        <el-row>
          <el-col :span="18">
            <el-form :inline="true" :model="requestParameters" >
              <el-form-item label="学科名称">
                <el-input v-model="requestParameters.subjectName"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="clear">清除</el-button>
                <el-button type="primary" size="small" @click="server">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
            <el-button style="float:right" type="success" icon="el-icon-edit" @click="showDialog = true">新增学科</el-button>
        </el-row>
      </div>
      <!-- 项目条数 -->
          <el-alert
            :title="alertText"
            type="info"
            class="alert"
            show-icon
            :closable="false"
          ></el-alert>
      <!-- 表格 -->
        <template>
          <el-table :data="tableData" style="width: 100%" border>
            <el-table-column label="序号" type="index" width="80" align="center" :index="indexMethod"/>
            <el-table-column prop="subjectName" label="学科名称"></el-table-column>
            <el-table-column prop="username" label="创建者"></el-table-column>
            <el-table-column prop="addDate" width="200"  label="创建日期">
              <template v-slot='{row}'>
                {{ row.addDate | parseTime }}
              </template>
            </el-table-column>
            <el-table-column prop="isFrontDisplay === 1? '是':'否'}" label="前台是否显示">
              <template v-slot='{row}'>
                {{ row.isFrontDisplay === 1 ? '是':'否'}}
              </template>
            </el-table-column>
            <el-table-column prop="twoLevelDirectory" width="100" label="二级目录"></el-table-column>
            <el-table-column prop="tags"  width="100" label="标签"></el-table-column>
            <el-table-column prop="totals" label="题目数量"></el-table-column>
            <el-table-column label="操作" width="280">
              <template v-slot="{row}">
                <el-button type="text" size="small" @click="$router.push(`/subjects/directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
                <el-button type="text" size="small" @click="$router.push(`/subjects/tags??id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
                <el-button type="text" size="small" @click="editSubjects(row)">修改</el-button>
                <el-button type="text" size="small" @click="deleteSubjects(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>

      <!-- 分页组件 -->
      <el-row type="flex" justify="end" style="margin-top: 20px">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Number(requestParameters.page)"
          :total="total"
          :page-size="Number(requestParameters.pagesize)"
          :page-sizes="[10,20,30, 50]"
          layout="prev, pager, next,sizes,jumper"
        ></el-pagination>
      </el-row>
    </el-card>
    <AddSubjects
      ref="AddSubjects"
      :showDialog.sync='showDialog'
      @closeShowDialog='closeShowDialog'
    >
    </AddSubjects>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import AddSubjects from '../components/subjects-add.vue'
export default {
  name: 'subjects',
  components: {
    AddSubjects
  },
  data () {
    return {
      loading: false,
      // 搜索内容框
      requestParameters: {
        // 学科名称
        subjectName: null,
        // 当前页
        page: 1,
        // 每页条数
        pagesize: 10
      },
      // 存放总数量
      total: null,
      alertText: '',
      // 存放信息
      tableData: [],
      // 新增学科弹窗
      showDialog: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 序号的优化
    indexMethod (index) {
      return (this.requestParameters.page - 1) * this.requestParameters.pagesize + index + 1
    },
    // 发送请求拿数据
    async getList (params) {
      this.loading = true
      const { data } = await list(this.requestParameters)
      this.tableData = data.items
      this.total = data.counts
      // console.log(data)
      this.alertText = `数据一共 ${this.total} 条`
      this.loading = false
    },
    // 每页显示信息的条数
    handleSizeChange (value) {
      this.requestParameters.pagesize = value
      if (this.requestParameters.page === 1) {
        this.getList(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange (value) {
      this.requestParameters.page = value
      this.getList()
    },
    // 搜索
    server () {
      this.requestParameters.page = 1
      this.getList()
    },
    // 清除
    clear () {
      this.requestParameters = {
        subjectName: null,
        page: 1,
        pagesize: 10
      }
      this.getList()
    },
    // 删除学科
    async deleteSubjects (row) {
      // console.log(row)
      try {
        await this.$confirm('此操作将永久删除该学科，是否继续？', '提示', { type: 'warning' })
      } catch {
        return
      }
      await remove(row)
      // console.log(res)
      if (this.tableData.length === 1) {
        this.requestParameters.page--
      }
      this.getList()
      this.$message.success('删除学科成功')
    },
    // 修改学科
    editSubjects (row) {
      this.showDialog = true
      this.$refs.AddSubjects.form = { ...row }
      // 自动获取焦点
      this.$nextTick(() => {
        this.$refs.AddSubjects.$refs.input.focus()
      })
    },
    // 关闭弹窗
    closeShowDialog () {
      this.showDialog = false
      this.$refs.AddSubjects.form = {
        subjectName: '',
        isFrontDisplay: 1,
        id: ''
      }
    }
  }
}
</script>

<style scoped lang='scss'>
  .container{
    margin: 10px 15px;
  }
  .alert {
  margin: 10px 0px;
  }
</style>
